#ifdef  __IPHONE_OS_VERSION_MAX_ALLOWED
#import <Foundation/Foundation.h>
#include "zluaobj.h"
extern "C" {
	#import "luasocket.h"
	int luaopen_lpeg (lua_State *L);
	int luaopen_luasql_sqlite3(lua_State *L);
	int luaopen_zpack(lua_State *L);
    int luaopen_bit(lua_State *L);
}
ZLuaEnvDelegateIOS::ZLuaEnvDelegateIOS()
{
	NSArray *paths;
	//paths = NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSSystemDomainMask, YES); 
	//m_path[ZLuaEnv::PATH_APP]=[[paths objectAtIndex:0] UTF8String];
//	NSString *filePath = [[NSBundle mainBundle] pathForResource:@"info.plist" ofType:@""]; 
//	m_path[ZLuaEnv::ZPATH_APP]=[filePath UTF8String];
//	m_path[ZLuaEnv::ZPATH_APP].findreplace("info.plist", "");
	NSString* filePath = [[NSBundle mainBundle] bundlePath];
	m_path[ZLuaEnv::ZPATH_APP]=[filePath UTF8String];
	m_path[ZLuaEnv::ZPATH_DEFAULT]=m_path[ZLuaEnv::ZPATH_APP];
	m_path[ZLuaEnv::ZPATH_ROOT]=[NSHomeDirectory() UTF8String];
	m_path[ZLuaEnv::ZPATH_TMP]=[NSTemporaryDirectory() UTF8String];
	paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSAllDomainsMask, YES); 
	zString doc_dir = [[paths objectAtIndex:0] UTF8String];
	doc_dir += "/";
	m_path[ZLuaEnv::ZPATH_DOC] = doc_dir;
	m_path[ZLuaEnv::ZPATH_CACHE] = doc_dir;
	//paths = NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSSystemDomainMask, YES); 
	//m_path[ZLuaEnv::ZPATH_APP]=[[paths objectAtIndex:0] UTF8String];
	m_path[ZLuaEnv::ZPATH_MOD]=m_path[ZLuaEnv::ZPATH_APP];
	
	for (int i=0;i<ZLuaEnv::ZPATH_MAXNUM;i++)
	{
		zString& f=m_path[i];
		if (f.length()==0 || f.c_str()[f.length()-1]!='/')
			f=f+'/';
		//fprintf(stderr,"%d=%s\n\n",i,m_path[i].c_str());
	}
}
const zString& ZLuaEnvDelegateIOS::GetPath(ZLuaEnv::EPATH p)
{
	if (p<ZLuaEnv::ZPATH_DEFAULT || p >=ZLuaEnv::ZPATH_MAXNUM)
		p=ZLuaEnv::ZPATH_DEFAULT;
	return m_path[p];
}

int ZLuaEnvDelegateIOS::OnCreateLuaEnv(ZLuaEnv& le)
{
	ZLuaEnvDelegate::OnCreateLuaEnv(le);
	zString rp="package.path='"+m_path[ZLuaEnv::ZPATH_MOD]+"lua/?.lua;"+m_path[ZLuaEnv::ZPATH_MOD]+"lualib/?.lua;'..package.path\npackage.cpath=''";
	le.dostring(rp);
	luaopen_zpack(le);
	le.dostring("package.loaded['zpack']={zpack=zpack,zunpack=zunpack}");
	luaopen_lpeg(le);
	luaopen_luasql_sqlite3(le);
	le.dostring("package.loaded['luasql.sqlite3']=luasql");
	luaopen_socket_core(le);
	le.dostring("package.loaded['socket.core']=socket; package.loaded['socket']=nil");
    luaopen_bit(le);
	return 0;
}
#endif